package io.esirong.widget.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import io.esirong.widget.R;


/**
 * 横向多功能View封装
 *
 * @author 黄嵘才(HuangRc)
 */
public class MenuItemView extends FrameLayout {
    private Context context;

    private ViewGroup content;
    private ImageView leftImg;
    private ImageView rightImg;
    private TextView leftText;
    private TextView rightText;
    private TextView centerText;

    private String leftTitle = null;
    private String rightTitle = null;
    private String centerTitle = null;

    private int leftImgIds;
    private int rightImgIds;

    private float imgLeftPadding;
    private float imgRightPadding;
    private float imgLeftMargin;
    private float imgRightMargin;

    private float leftTvSize;
    private float rightTvSize;
    private float centerTvSize;

    private int leftTvColor;
    private int rightTvColor;
    private int centerTvColor;

    private boolean showLeftImg;
    private boolean showRightImg;

    public MenuItemView(Context context) {
        super(context);
        this.context = context;
    }

    public MenuItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context, attrs);
    }

    public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        LayoutInflater.from(context).inflate(R.layout.layout_menu_options, this, true);
        leftImg = findViewById(R.id.iv_left);
        rightImg = findViewById(R.id.iv_right);
        leftText = findViewById(R.id.tv_title_left);
        rightText = findViewById(R.id.tv_title_right);
        centerText = findViewById(R.id.tv_title_center);
        content = findViewById(R.id.rl_content);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MenuItemView);

        try {
            leftTitle = typedArray.getString(R.styleable.MenuItemView_title_left);
            rightTitle = typedArray.getString(R.styleable.MenuItemView_title_right);
            centerTitle = typedArray.getString(R.styleable.MenuItemView_title_center);
            leftImgIds = typedArray.getResourceId(R.styleable.MenuItemView_img_left_src, -1);
            rightImgIds = typedArray.getResourceId(R.styleable.MenuItemView_img_right_src, -1);

            imgLeftPadding = typedArray.getDimension(R.styleable.MenuItemView_img_left_padding, 0);
            imgRightPadding = typedArray.getDimension(R.styleable.MenuItemView_img_right_padding, 0);
            imgLeftMargin = typedArray.getDimension(R.styleable.MenuItemView_img_left_margin, 0);
            imgRightMargin = typedArray.getDimension(R.styleable.MenuItemView_img_right_margin, 0);
            leftTvSize = typedArray.getDimension(R.styleable.MenuItemView_left_tv_size, sp2px(14));
            rightTvSize = typedArray.getDimension(R.styleable.MenuItemView_right_tv_size, sp2px(14));
            centerTvSize = typedArray.getDimension(R.styleable.MenuItemView_center_tv_size, sp2px(14));

            leftTvColor = typedArray.getColor(R.styleable.MenuItemView_left_text_color, leftText.getCurrentTextColor());
            rightTvColor = typedArray.getColor(R.styleable.MenuItemView_right_text_color, centerText.getCurrentTextColor());
            centerTvColor = typedArray.getColor(R.styleable.MenuItemView_center_text_color, rightText.getCurrentTextColor());

            showLeftImg = typedArray.getBoolean(R.styleable.MenuItemView_show_left_icon, true);
            showRightImg = typedArray.getBoolean(R.styleable.MenuItemView_show_right_icon, true);
        } finally {
            typedArray.recycle();
        }
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        tvStatChange();
        if (leftImgIds > 0) {
            leftImg.setImageResource(leftImgIds);
        }
        if (rightImgIds > 0) {
            rightImg.setImageResource(rightImgIds);
        }
        leftText.setText(leftTitle);
        rightText.setText(rightTitle);
        centerText.setText(centerTitle);

        leftText.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTvSize + 1);
        rightText.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTvSize + 1);
        centerText.setTextSize(TypedValue.COMPLEX_UNIT_PX, centerTvSize + 1);

        if (leftTvColor != centerText.getCurrentTextColor()) {
            setLeftTextColor(leftTvColor);
        }
        if (centerTvColor != centerText.getCurrentTextColor()) {
            setCenterTextColor(centerTvColor);
        }
        if (rightTvColor != rightText.getCurrentTextColor()) {
            setRightTextColor(rightTvColor);
        }

        leftImg.setPadding((int) imgLeftPadding, (int) imgLeftPadding, (int) imgLeftMargin, (int) imgLeftPadding);
        rightImg.setPadding((int) imgRightMargin, (int) imgRightPadding, (int) imgRightPadding, (int) imgRightPadding);

        leftImg.setVisibility(showLeftImg ? VISIBLE : GONE);
        rightImg.setVisibility(showRightImg ? VISIBLE : GONE);
    }

    /**
     * 动态设置3个TextView的显示和隐藏
     */
    public void tvStatChange() {

    }

    public void setLeftText(String str) {
        leftTitle = str;
        leftText.setText(leftTitle);
        tvStatChange();
    }

    public void setRightText(String str) {
        rightTitle = str;
        rightText.setText(rightTitle);
        tvStatChange();
    }

    public void setCenterText(String str) {
        centerTitle = str;
        centerText.setText(centerTitle);
        tvStatChange();
    }

    public String getLeftTitle() {
        return leftTitle;
    }

    public String getRightTitle() {
        return rightTitle;
    }

    public String getCenterTitle() {
        return centerTitle;
    }

    public void setLeftImgSrc(int id) {
        leftImg.setImageResource(id);
    }

    public void setRightImgSrc(int id) {
        rightImg.setImageResource(id);
    }

    public void setLeftTextColor(@ColorInt int color) {
        leftText.setTextColor(color);
    }

    public void setCenterTextColor(@ColorInt int color) {
        centerText.setTextColor(color);
    }

    public void setRightTextColor(@ColorInt int color) {
        rightText.setTextColor(color);
    }

    private int dp2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private int sp2px(float spValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spValue, context.getResources().getDisplayMetrics());
    }
}
